package com.example.mqdemo.subscribe;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/*
 *@description: 发布/订阅模式配置文件
 *@author: hisen
 *@Time: 2023/11/22  17:14
 */
@Configuration
public class SubscribeConfig {
    //两个订阅队列
    @Bean("subscribeQueue1")
    public Queue subscribeQueue1() {
        return new Queue("subscribe1");
    }

    @Bean("subscribeQueue2")
    public Queue subscribeQueue2() {
        return new Queue("subscribe2");
    }

    //扇形交换机
    @Bean("subscribeExchange")
    public FanoutExchange subscribeExchange() {
        return new FanoutExchange("subscribeExchange");
    }

    //绑定两个subscribeQueue队列到subscribeExchange交换机
    @Bean
    public Binding subscribeBinding1(
            @Qualifier("subscribeQueue1") Queue subscribeQueue,
            @Qualifier("subscribeExchange") FanoutExchange subscribeExchange) {
        return BindingBuilder.bind(subscribeQueue).to(subscribeExchange);
    }

    @Bean
    public Binding subscribeBinding2(
            @Qualifier("subscribeQueue2") Queue subscribeQueue,
            @Qualifier("subscribeExchange") FanoutExchange subscribeExchange) {
        return BindingBuilder.bind(subscribeQueue).to(subscribeExchange);
    }
}
